package com.justnow.offer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author justnow
 * Created on 2020-08-24
 * Description
 */
public class Solution14 {
    public void levelSort(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<Integer> array = new ArrayList<>();
        queue.add(root);
        while (queue.size() > 0) {
            TreeNode current = queue.poll();
            array.add(current.val);
            if (current.left != null) queue.add(current.left);
            if (current.right != null) queue.add(current.right);
        }
        for (Integer integer : array) {
            System.out.println(integer);
        }
    }

    public static void main(String[] args) {
        Solution14 solution14 = new Solution14();
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;
        solution14.levelSort(node1);
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode (int val) {
        this.val = val;
    }
}